package org.mat.ipaas.ucenter.bff.admin.modules.system;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import me.zhyd.oauth.model.AuthCallback;
import org.mat.framework.lang.dto.MatApiResponse;
import org.mat.ipaas.ucenter.service.api.system.ThirdLoginRestAPI;
import org.mat.ipaas.ucenter.service.dto.system.model.ThirdLoginModelDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author scott
 * @since 2018-12-17
 */
@Controller
@RequestMapping("/sys/thirdLogin")
@Slf4j
public class ThirdLoginController {

    @Autowired
    private ThirdLoginRestAPI thirdLoginRestAPI;

    @RequestMapping("/render/{source}")
    public void render(@PathVariable("source") String source) throws IOException {
        thirdLoginRestAPI.render(source);
    }

    @RequestMapping("/{source}/callback")
    public String loginThird(@PathVariable("source") String source, AuthCallback callback, ModelMap modelMap) {
        String ret = thirdLoginRestAPI.loginThird(source, callback, modelMap);
        return ret;
    }

    /**
     * 创建新账号
     *
     * @param model
     * @return
     */
    @PostMapping("/user/create")
    @ResponseBody
    public MatApiResponse<String> thirdUserCreate(@RequestBody ThirdLoginModelDTO model) {
        MatApiResponse<String> matApiResponse = thirdLoginRestAPI.thirdUserCreate(model);
        return matApiResponse;
    }

    /**
     * 绑定账号 需要设置密码 需要走一遍校验
     *
     * @param json
     * @return
     */
    @PostMapping("/user/checkPassword")
    @ResponseBody
    public MatApiResponse<String> checkPassword(@RequestBody JSONObject json) {
        MatApiResponse<String> matApiResponse = thirdLoginRestAPI.checkPassword(json);
        return matApiResponse;
    }

    /**
     * 第三方登录回调接口
     *
     * @param token
     * @param thirdType
     * @return
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/getLoginUser/{token}/{thirdType}", method = RequestMethod.GET)
    @ResponseBody
    public MatApiResponse<JSONObject> getThirdLoginUser(@PathVariable("token") String token, @PathVariable("thirdType") String thirdType) throws Exception {
        MatApiResponse<JSONObject> matApiResponse = thirdLoginRestAPI.getThirdLoginUser(token, thirdType);
        return matApiResponse;
    }

    /**
     * 第三方绑定手机号返回token
     *
     * @param jsonObject
     * @return
     */
    @ApiOperation("手机号登录接口")
    @PostMapping("/bindingThirdPhone")
    @ResponseBody
    public MatApiResponse<String> bindingThirdPhone(@RequestBody JSONObject jsonObject) {
        MatApiResponse<String> matApiResponse = thirdLoginRestAPI.bindingThirdPhone(jsonObject);
        return matApiResponse;
    }

    /**
     * 企业微信/钉钉 OAuth2登录
     *
     * @param source
     * @param state
     * @return
     */
    @ResponseBody
    @GetMapping("/oauth2/{source}/login")
    public String oauth2LoginCallback(@PathVariable("source") String source,
                                      @RequestParam("state") String state) throws Exception {
        String ret = thirdLoginRestAPI.oauth2LoginCallback(source, state);
        return ret;
    }

    /**
     * 企业微信/钉钉 OAuth2登录回调
     *
     * @param code
     * @param state
     * @return
     */
    @ResponseBody
    @GetMapping("/oauth2/{source}/callback")
    public String oauth2LoginCallback(
            @PathVariable("source") String source,
            // 企业微信返回的code
            @RequestParam(value = "code", required = false) String code,
            // 钉钉返回的code
            @RequestParam(value = "authCode", required = false) String authCode,
            @RequestParam("state") String state) {
        String ret = thirdLoginRestAPI.oauth2LoginCallback(source, code, authCode, state);
        return ret;
    }

}